//  this library.
import 'package:flutter/material.dart';
import 'package:hktk_app/dao/my_dao.dart';
import 'package:hktk_app/exception/dao_exception.dart';
import 'package:hktk_app/ienum/my/rank_type_enum.dart';
import 'package:hktk_app/ienum/practice/ct_enum.dart';
import 'package:hktk_app/ienum/question/answer_type_enum.dart';
import 'package:hktk_app/ienum/question/question_type_enum.dart';
import 'package:hktk_app/ienum/subject/collection_type_enum.dart';
import 'package:hktk_app/model/data_model.dart';
import 'package:hktk_app/model/practice/practice_question_model.dart';
import 'package:hktk_app/model/question/paper_data_model.dart';
import 'package:hktk_app/model/question/question_data_model.dart';
import 'package:hktk_app/model/subject/question_collection_model.dart';
import 'package:hktk_app/pages/question/child/question_exam_page.dart';
import 'package:hktk_app/util/data_util.dart';
import 'package:hktk_app/util/my_util.dart';

/// 我的错题/收藏。章节/试卷试题显示页面
class CollectionQuestionPage extends StatefulWidget {
  /// 集合id
  final String collectId;

  /// 集合类型
  final CollectionTypeEnum collectionType;

  /// 是错题还是收藏
  final RankTypeEnum type;

  const CollectionQuestionPage({
    super.key,
    required this.collectId,
    required this.collectionType,
    required this.type,
  });

  @override
  State<CollectionQuestionPage> createState() => _CollectionQuestionPageState();
}

class _CollectionQuestionPageState extends State<CollectionQuestionPage> {
  /// 试题集合
  late Map<String, QuestionDataModel> _dataMap;

  @override
  Widget build(BuildContext context) {
    debugPrint("collectionQuestionPage 页面 -> build 方法执行 ===> ");
    debugPrint("");
    return QuestionExamPage(
      paperInit: _initData,
      quesInit: _quesInit,
      type: QuestionTypeEnum.examEnd,
    );
  }

  /// 加载数据方法
  Future<Map<String, dynamic>> _initData() async {
    Map<String, dynamic> map = {};
    try {
      DataModel dataModel = dataUtilComm.dataModel;
      QuestionCollectionModel model = await MyDao.collectCollectionNew(
        industryCode: dataModel.industryId,
        examId: dataModel.examId,
        categoryId: dataModel.categoryId,
        collectId: widget.collectId,
        collectionType: widget.collectionType,
        type: widget.type,
      );
      PaperDataModel paperDataModel = _getPaperData(model);
      map["data"] = paperDataModel;
      map["suc"] = true;
    } catch (e) {
      debugPrint("collectionQuestionPage 页面 -> 章节加载试题相关数据方法发生异常 ===> $e");
      map = {"suc": false, "msg": "未知错误，请重试"};
      if (e is DaoException) {
        debugPrint("collectionQuestionPage 页面 -> dao异常 ===> ${e.msg}");
        map["msg"] = e.msg;
      }
    }
    return map;
  }

  /// 获取对应的试题数据方法
  PaperDataModel _getPaperData(QuestionCollectionModel model) {
    List<DtkInfoModel> dtkList = [];
    List<BogInfoModel> bogList = [];
    _dataMap = {};
    List<ContentCollection> content = model.content!;
    int i = 0;
    for (ContentCollection content in content) {
      bogList.add(BogInfoModel(
        name: content.tyName!,
        type: content.qs![0].firstType!,
        total: content.qs?.length ?? 0,
        dtkList: [],
      ));
      List<PracticeQuestionModel> qsList = content.qs!;
      for (PracticeQuestionModel qModel in qsList) {
        // 试题数据
        QuestionDataModel qDataModel = MyUtil.getQuestionData(qModel);
        _dataMap[qModel.id!] = qDataModel;
        // 本题是否已做
        AnswerTypeEnum ansType = AnswerTypeEnum.notDone;
        List<String> ans = [];
        if (qModel.record != null) {
          RecordModel record = qModel.record!;
          ansType = record.rt! ? AnswerTypeEnum.success : AnswerTypeEnum.error;
          ans = MyUtil.getUserAnsList(record.as, qModel.firstType!);
        }

        // 答题卡相关数据
        dtkList.add(DtkInfoModel(
          index: i,
          id: qModel.id!,
          type: qModel.firstType!,
          sign: qModel.sign!,
          ansType: ansType,
          userAns: ans,
        ));
        i++;
      }
    }
    return PaperDataModel(
      name: model.name!,
      id: model.id!,
      collectId: widget.collectId,
      lastIndex: 0,
      dtkList: dtkList,
      bogDtkList: bogList,
      rType: model.recordsType ?? CtEnum.hotQue,
      startDate: DateTime.now().millisecondsSinceEpoch,
    );
  }

  /// 加载试题数据方法
  Future<Map<String, dynamic>> _quesInit(String qId, String cid) async {
    QuestionDataModel? dataModel = _dataMap[qId];
    if (dataModel == null) {
      return {"suc": false, "msg": "未知错误，请重试"};
    }
    return {"suc": true, "data": dataModel};
  }
}
